<?php
define('QUALITY',100);

function resize_image($img_file,$large)
{
	if(!function_exists('imagecreatefromgif'))
		die('no esta instalado');
	// Ocultamos el error que puede ser generado por el usuario al pasar una imagen no valida.
	$origin = 0;
	switch ($img_file['type'])
	{
		case 'image/gif':
			$origin    = @imagecreatefromgif($img_file['tmp_name']);
			break;
			
		case 'image/png':
			$origin    = @imagecreatefrompng($img_file['tmp_name']);
			break;
			
		case 'image/jpeg':
			$origin    = @imagecreatefromjpeg($img_file['tmp_name']);
			break;
					
		case 'image/pjpeg': // Opera confuse usres only.
			$origin    = @imagecreatefromjpeg($img_file['tmp_name']);
			break;
		
		default: // Borramos el archivo temporal.
			@unlink($img_file['tmp_name']);
			return False;
	}
	if (!$origin) // Borramos el archivo temporal.
	{
		@unlink($this->file['tmp_name']);
		return False;
	}
	
	// Imagenes de destino.
	$destSmall  = ImageCreateTrueColor($large,$large);
	
	// Antialias.		
	/*
	imageantialias($destSmall,true);
	*/
	imageinterlace($destSmall,true);
	
	// Color de fondo blanco		
	imagefill($destSmall, 0,0,imagecolorallocate($destSmall,255,255,255));
	
	// Mantenemos el ratio.		
	$ssize = get_resize_dimensions(imagesy($origin),imagesx($origin),$large,$large);

	imagecopyresampled($destSmall, $origin,($large  - $ssize['x']) / 2,($large  - $ssize['y']) / 2,0,0,$ssize['x'],$ssize['y'],imagesx($origin),imagesy($origin));

	ob_start();	
	imagejpeg($destSmall,'',QUALITY);
	return ob_get_clean();
}
			
function get_resize_dimensions($current_width,$current_height, $desired_width, $desired_height)
{
	$liChange  = 0;
	$newHeight = 0;
	$newWidth  = 0;
	if ($current_height == $current_width) // Squera
	{
		$liChange  = $desired_height / $current_height;
		$newHeight = $current_height * $liChange;
		$newWidth  = $current_width * $liChange;
	}
	else if ($current_height < $current_width)
	{
		$liChange  = $desired_width /  $current_width;
		$newHeight = $current_height * $liChange;
		$newWidth  = $desired_width;
	}
	else
	{
		$liChange  = $desired_height / $current_height;
		$newHeight = $desired_height;	
		$newWidth  = $current_width * $liChange;
	}
	return $sise = array(
		'x' => $newHeight,
		'y' => $newWidth
		);
}
